# -*- encoding: utf-8 -*-
'''
@File    :   selected_stock_K_line.py    
@Contact :   ypy02784@163.com
@License :   (C)Copyright 2019-2020, ypy

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020/3/3 23:42   ypy      1.0         None
'''


from stock import models
from django.core import serializers
from django.forms.models import model_to_dict
import requests
from bs4 import BeautifulSoup
import pandas as pd


pd.set_option('display.max_rows', None)


def get_chart_moneyflow_from_eastmoney(ts_code):
    return get_chart_moneyflow_from_eastmoney(ts_code)


def get_new_moneyflow(ts_code):
    new_moneyflow = models.moneyflow.objects.filter(ts_code=ts_code).order_by("-trade_date")[0]
    new_moneyflow = model_to_dict(new_moneyflow)
    return  new_moneyflow



def get_url(ts_code):  # 根据期数创建url
    # eastmoeny
    url = "http://quote.eastmoney.com/" + ts_code.strip() + ".html"
    return url

def get_zjlx_url(ts_code):
    # ts_code = ts_code[2:]
    url = "http://finance.sina.com.cn/realstock/company/"+ts_code.strip()+"/nc.shtml"
    # url = "http://data.eastmoney.com/zjlx/" + ts_code.strip() + ".html"
    return url


def _get_html(urls):
    response = requests.get(urls)
    # print(response.text)
    if response.status_code == 200:  # 有数据则返回的为200
        code = response.encoding
        str = response.text
        str = str.encode(code)
        str = str.decode('GBK')
        return str

    else:
        print('无数据！')
    return None


def get_chart_moneyflow_from_eastmoney(ts_code):
    url = get_url(ts_code)
    html = _get_html(url)
    bf = BeautifulSoup(html, "html.parser")
    # eastmoney
    # s = bf.find_all("div",{'class':"w330 fl mt10"})
    chart_money = bf.find_all("iframe", id='iframebingtu')  #返回资金流图

    # url_zjlx = get_zjlx_url(ts_code)
    # html_zjlx = _get_html(url_zjlx)
    #
    # # print(html_zjlx)
    # bf_zjlx = BeautifulSoup(html_zjlx, "html.parser")
    # # main_in = bf_zjlx.find("span", id="data_jlr").get_text()
    # # main_out = bf_zjlx.find("span", id="data_jzb").get_text()
    # # print(main_in + '  and  ' + main_out  )
    # b = bf_zjlx.find("div", id="MRFlow")
    # print(b)
    return chart_money
